<!DOCTYPE html>
<html lang="en">

	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
		<meta name="description" content="">
		<meta name="author" content="">
		<!-- STYLESHEETS -->
		<!--[if lt IE 9]><script src="../../js/flot/excanvas.min.js"></script><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
		<link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
		<link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
		<link rel="stylesheet" type="text/css" href="../../css/responsive.css">

		<link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
		<!-- file-input -->
		<link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css" />
		<!-- SELECT2 -->
		<link rel="stylesheet" type="text/css" href="../../js/select2/select2.min.css" />
		<!-- UNIFORM -->
		<link rel="stylesheet" type="text/css" href="../../js/uniform/css/uniform.default.min.css" />
		<!-- datatable -->
		<link rel="stylesheet" href="../../js/datatables/datatable.css">
		<!-- WIZARD -->
		<link rel="stylesheet" type="text/css" href="../../js/bootstrap-wizard/wizard.css" />
		  <link rel="stylesheet" href="../../js/datepicker/datepicker.css">
		<!-- FONTS 
	<link href='http://fonts.useso.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
-->
		<!-- JQUERY -->
		<script src="../../js/jquery/jquery-2.0.3.min.js"></script>
		<script src="../../js/common.js"></script>
	</head>

	<body>
		<header class="navbar clearfix" id="header">
		</header>

		<!-- PAGE -->
		<section id="page">
			<!-- SIDEBAR -->
			<div id="sidebar" class="sidebar">
				<div class="sidebar-menu nav-collapse">

					<!-- SIDEBAR MENU -->
					<ul>
						
					</ul>
					<!-- /SIDEBAR MENU -->
				</div>

			</div>
			<!-- /SIDEBAR -->
			<div id="main-content">

				<div class="container">
					<div class="row">
						<div id="content" class="col-lg-12">
							<!-- PAGE HEADER-->
							<div class="row">
								<div class="col-sm-12">
									<div class="page-header">
										<!-- STYLER -->

										<!-- /STYLER -->
										<!-- BREADCRUMBS -->
										<ul class="breadcrumb">
											<li>
												<i class="fa fa-home"></i>
												<a href="index.html">首页</a>
											</li>
											<li>
												<a href="#">商品管理</a>
											</li>
											<li>维护商品</li>
										</ul>
										<!-- /BREADCRUMBS -->

									</div>
								</div>
							</div>
							<!-- /PAGE HEADER -->
							<!-- SAMPLE -->
							<form id="form_promotion">
							
							<div class="row col-md-12">
								<div class="box border primary" style="margin-left: 20px;">
											<div class="box-title">
												<h4><i class="fa fa-bars"></i>商品成本维护</h4>
											</div>
											<div class="box-body big" style="padding:10px;height: 400px">
								
										<div class="form-group" style="margin-bottom:10px">
								<input type="hidden" name=id>
									<label class="control-label col-md-3">商品名称：</label>
									<div class="col-md-8 productName">
								    </div>
								</div>
								<div class="form-group" style="margin-bottom:10px">
									<label class="control-label col-md-3">成衣价格：</label>
									<div class="col-md-3">
									<input type="text" class="form-control" name="skuprice"  onkeypress="return $.IsDecimal(event);" onblur="return $.IsMoney(this);"  placeholder="请输入单价" maxlength="20"/>
								    </div>
								</div>
									<div class="form-group" style="margin-bottom:10px">
									<label class="control-label col-md-3">包装袋：</label>
									<div class="col-md-3">
									<select  id="baozhuang_select"  class="col-md-12 select2-offscreen" tabindex="-1">
									</select>
								    </div>
								</div>
								<div class="form-group" style="margin-bottom:10px">
									<label class="control-label col-md-3">封口贴：</label>
									<div class="col-md-3">
									<select  id="fengkou_select"  class="col-md-12 select2-offscreen" tabindex="-1">
									</select>
								    </div>
								</div>
								<div class="form-group divtiezhi" style="margin-bottom:10px">
									<label class="control-label col-md-3">贴纸：</label>
									<div class="col-md-8" style="display:inline-flex">
									<table class="table table-bordered tiezhi">
									</table>
									
								    </div>
								</div>
								
								<div class="form-group" style="margin-bottom:0px">
									<label class="control-label col-md-3"><span class="required">*</span>SKU：</label>
									<div class="col-md-9">
									<table class="table table-bordered skus">
									<thead>
									<tr>
									<th style="display:none">id</th>
									<th>尺码/大小</th>
									<th style="width:100px">SKU编码</th>									
									<th style="width:100px">成衣成本(元)</th>
									<th>包装成本(元)</th>
									<th>总成本(元)</th>
									</tr>
									</thead>
									<tbody><tr>
									<td></td>
									<td></td>
									<td><input type="text" class="form-control" maxlength="10"  onkeypress="return $.IsDecimal(event);" onblur="return $.IsMoney(this);"/></td>
									<td class="baozhuang_price"></td>
									<td>asda</td>
									</tr>
									</tbody>
									
									</table>
								    </div>
								</div>
								
								
								
								
										
							</div>
									
							  </div>
						    </div>	
							</form>
							<div class="row" style="height: 30px;">
								<div class="col-md-12" style="text-align: center;">
									<div class="box ">
										<a href="javascript:;"  class="btn btn-success config bt-submit" style="width: 100px">
											保存
										</a>
										<a href="product_cost.html"  class="btn btn-default" style="width: 100px">
											返回
										</a>
									</div>
								</div>
					           </div>	
									</div>
								</div>
										
							
							<!-- /SAMPLE -->

							<div class="footer-tools">
								<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
							</div>
						</div>
						<!-- /CONTENT-->
					</div>
				</div>
			</div>
		</section>
		<!--/PAGE -->
		<!-- JAVASCRIPTS -->
		<!-- Placed at the end of the document so the pages load faster -->

		<!-- BOOTSTRAP -->
		<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>

		<!-- BLOCK UI -->
		<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
		<!--bootbox-->
		<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
		<!-- AUTOSIZE -->
		<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>
		<!-- spinner -->
		<script src="../../js/spinner/spin.js"></script>
		<!-- INPUT MASK -->
		
		<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>
			<!-- UNIFORM -->
	<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
		<!-- DATA TABLES -->
		<script type="text/javascript" src="../../js/datatables/bootstrap-select.js"></script>
		<script type="text/javascript" src="../../js/datatables/datatable.js"></script>
		<script type="text/javascript" src="../../js/datatables/datatable-bootstrap.js"></script>
		<script type="text/javascript" src="../../js/datatables/datatable-tabletools.js"></script>
		<script type="text/javascript" src="../../js/datatables/fnAddTr.js"></script>


		<!-- SELECT2 -->
		<script type="text/javascript" src="../../js/select2/select2.min.js"></script>
	
        <script type="text/javascript" src="../../js/datepicker/datepicker.js"></script>
        <script src="../../js/jquery-validate/jquery.validate.min.js"></script>
		<!-- COOKIE -->
		<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
		<!-- CUSTOM SCRIPT -->
		<script src="../../js/script.js"></script>

		<script src="../../js/Sortable.min.js"></script>
		<script>
			var AddCost = function() {
				var productName,productCode;
				var fengkou,tiezhi,baozhuang,num=1;
		        var getMaterial=function(){
		        	
		        	$.ajax({
						type: "post",
						url: App.getContextPath() + "admin/skumaterial/searchMaterialList.do",					
						async: true,
						success: function(result) {
								if(result.length>0){
									
									var arr=[];
									
									$.each(result,function(index,item){
										if(item.categoryName=='封口贴')
											fengkou+='<option value='+item.id+' price='+item.price+'>'+item.price+"("+item.materialName+')</option>';
										if(item.categoryName=='包装袋')
											baozhuang+='<option value='+item.id+' price='+item.price+'>'+item.price+"("+item.materialName+')</option>';
										if(item.categoryName=='贴纸')
											{//tiezhi+='<option value='+item.price+'>'+item.materialName+'</option>';
											arr.push(item);
											}
											});
									$("#baozhuang_select").append(baozhuang).select2();
									
									$("#fengkou_select").append(fengkou).select2();
									
									$("select").change(cumBaozhuang);
								
									tiezhi="<thead>";
									for(var i=0;i<arr.length;i++){
										tiezhi+='<th>'+arr[i].materialName+"</th>";
									}
									tiezhi+="</thead><tr>"
									for(var i=0;i<arr.length;i++){
										tiezhi+='<td class="tiezhitd"><table><tr><td class="tiezhiprice" >'+arr[i].price+"*</td><td><input type='text' class='form-control' id="+arr[i].id+" maxlength='1' style='width:50px' onkeypress='return $.IsNum(event);' onblur='return $.IsNaN(this);' /></td></tr></table></td>";
									}
									tiezhi+="</tr>";
									$(".tiezhi").append(tiezhi);
									
									$(".tiezhi").find("input").blur(cumBaozhuang);
								}						
							}
						
					});
		        }
			
	        var cumBaozhuang=function(){
	        	var baozhuang=$("#baozhuang_select option:selected").attr("price");
	        	var fengkou=$("#fengkou_select option:selected").attr("price");
	        	var tiezhi=0;
	        	$(".tiezhitd").each(function(){	        	
	        		var b=$(this)[0].innerText.replace('*','');
	        		var p=$(this).find("input").val();
	        		if(p!=""){
	        			tiezhi+=parseFloat(b)*parseInt(p);
	        		}	        		
	        	});
	        	var total=parseFloat(baozhuang)+parseFloat(fengkou)+parseFloat(tiezhi);
	        	$(".skus tbody tr").each(function(){	        	
	        		$(this).find("td:eq(4)").html(total.toFixed(2));
	        		$(this).find("td:eq(5)").html((total+parseFloat($(this).find("[name=skuprice]").val())).toFixed(2));
	        	});
	        }
	        var getSku=function(){
	        	 //根据产品id 获取SKu 信息
	        	 $.ajax({
						type: "post",
						url: App.getContextPath() + "admin/skumaterial/getSkuCostListByProductId.do",	
						data:{productCode:productCode},
						async: true,
						success: function(result) {
							if(result.length>0){
							//获取sku				        	
				        	 var str="";
							var h=34;
				        	$.each(result,function(index,item){
				        		h+=34;
				        		//获取第1行的ext
				        		var skuPrice="0";
				        		if(item.extMaterial!=null&&item.extMaterial!=""){
				        			
					        	var ext=JSON.parse(item.extMaterial);
					        	if(typeof ext.skuCost!="undefined")
					        	 skuPrice=ext.skuCost;
					        	 
				        		}
				        		
				        		str+='<tr><td class="id" style="display:none"></td><td>'+item.ygSku.sizeName+'</td><td>'+item.skuCode+'</td><td><input type="text" name=skuprice class="form-control" maxlength="10" value='+skuPrice+'  onkeypress="return $.IsDecimal(event);" onblur="return $.IsMoney(this);"/></td><td></td><td></td>'
				        	});
				        	h+=parseInt($(".box-body").css("height").replace("px",''));
				        	$(".box-body").css("height",h);
				        	$(".skus tbody").empty().append(str);
				        	$("[name=skuprice]").blur(function(){													
								cumBaozhuang();
							});				        	
				        	//获取第1行的ext	
				        	if(result[0].extMaterial!=null&&result[0].extMaterial!=""){
				        	var m=JSON.parse(result[0].extMaterial);
				        	if(typeof m.baozhuang!="undefined")
				        		
				        	 $("#baozhuang_select").select2("val",m.baozhuang.id);
				        	if(typeof m.fengkou!="undefined")
				        	 $("#fengkou_select").select2("val",m.fengkou.id);
				        	 $.each(m.tiezhi,function(index,item){				        		
				        		 $(".tiezhi").find("input[id="+item.id+"]").val(item.amount);
				        	 });
				        	}
				        	setTimeout(cumBaozhuang,500);
				        	
						  }
						}
						});
	        }
	        var updatePrice=function(){
	        	
	        	 getMaterial();	        	 
	        	 $(".productName").html(productName);
	        	 setTimeout(getSku,100);
	        	 $(".bt-submit").unbind("click").click(function(){
	        		 var p=[];
	        		 var material={};
        			 material.baozhuang={id:$("#baozhuang_select option:selected").val(),price:$("#baozhuang_select option:selected").attr("price")};
        			 material.fengkou={id:$("#fengkou_select option:selected").val(),price:$("#fengkou_select option:selected").val()};
        			 material.tiezhi=[];
        			 //循环贴纸
        			 $(".tiezhitd").each(function(){
        				 var t={};
        				 var p=$(this)[0].innerText.replace('*','')
        				var a=$(this).find("input").val();
        				if(a!=""){
        				 t.id=$(this).find("input").attr("id");
        				 t.price=parseFloat(a)*parseInt(p);
        				 t.amount=a;
        				 material.tiezhi.push(t);
        				}       		
	        		  });
        			
	        		 $.each($(".skus tbody tr"),function(index,item){
	        			 var sku={};	
	        			 sku.productId=0;
	        			 sku.productCode=productCode;	        			 
	        			 sku.skuCode=$(item).find("td:eq(2)").html();
	        			 sku.costPrice=$(item).find("td:eq(5)").html();
	        			 material.skuCost=$(item).find("input").val();
	        			 sku.extMaterial=JSON.stringify(material);
	        			 p.push(sku);
	        		 });
	        		 
	        		
	        		 $.ajax({
							type: "post",
							url: App.getContextPath() + "admin/skumaterial/savaSkuCost.do",
							data:$.param({skus:JSON.stringify(p)}),
							async: true,
							success: function(result) {
								$.alert("更新成功");
								window.location.href="product_cost.html";
								}
							
						});
	        		
	        		 
	        	 });
	         }
				return {
					init: function() {
					     App.init();	
					     productCode=$.getUrlParam("productCode");
						 productName=$.getUrlParam("productName");
						 updatePrice();
						$("[name=skuprice]").blur(function(){
							$(".skus tbody").find("input").val($(this).val());						
							cumBaozhuang();
						});
						$("[name=price]").blur(cumBaozhuang);
						
					}
				}
			}();
			$(function() {
				$("#header").load("../head.html");
				AddCost.init();
			});
		</script>

		<!-- /JAVASCRIPTS -->
	</body>

</html>